package View;

import java.awt.BorderLayout;
import java.awt.Dialog.ModalExclusionType;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JScrollPane;

import Controller.ControladorCategoria;
import Model.Category;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProcesarCategoria extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private TableModel tmodelo;
	private List<Category> datos;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProcesarCategoria frame = new ProcesarCategoria();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void actualizarModelo(JTable table) {
		ControladorCategoria cont = new ControladorCategoria();
		datos = cont.cargarTodos();
		
		tmodelo = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public int getColumnCount() {
				return 3;
			}

			@Override
			public String getColumnName(int col) {
				switch (col) {
				case 0:
					return "Codigo";
				case 1:
					return "Nombre";
				case 2:
					return "Descripcion";
				}
				return null;
			}

			@Override
			public int getRowCount() {
				return datos.size();
			}

			@Override
			public Object getValueAt(int row, int col) {
				Category obj = datos.get(row);
				switch (col) {
				case 0:
					return obj.getCodCateg();
				case 1:
					return obj.getNombreC();
				case 2:
					return obj.getDescripcion();
				}
				return null;
			}
		};

		table.setModel(tmodelo);
	}
	
	/**
	 * Create the frame.
	 */
	public ProcesarCategoria() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btnMod = new JButton("Upd");
		btnMod.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int fila = table.getSelectedRow();
				ActualizarCategoria act = new ActualizarCategoria(datos.get(fila));		
				act.setVisible(true);
			
			}
		});
		btnMod.setBounds(33, 216, 89, 23);
		contentPane.add(btnMod);

		JButton btnDel = new JButton("Del");
		btnDel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ControladorCategoria controladorCategoria = new ControladorCategoria();
				int fila = table.getSelectedRow();
				controladorCategoria.Eliminar(datos.get(fila));
				actualizarModelo(table);
			}
		});
		btnDel.setBounds(132, 216, 89, 23);
		contentPane.add(btnDel);

		JButton btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ProcesarCategoria.this.dispose();
			}
		});
		btnClose.setBounds(229, 216, 89, 23);
		contentPane.add(btnClose);

		JScrollPane jScrollPane1 = new JScrollPane();
		contentPane.add(jScrollPane1);
		jScrollPane1.setBounds(24, 47, 383, 146);
		{
			table = new JTable();
			jScrollPane1.setViewportView(table);
			table.setBounds(49, 53, 288, 113);
		}
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GestionarCategoria cat = new GestionarCategoria();		
				cat.setVisible(true);
				cat.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
				actualizarModelo(table);
			}
		});
		btnAdd.setBounds(328, 216, 89, 23);
		contentPane.add(btnAdd);
		
		actualizarModelo(table);
	}
}
